package com.darkplanet.management.controller;

import com.darkplanet.management.domain.Employee;
import com.darkplanet.management.domain.SecondMenu;
import com.darkplanet.management.service.EmployeeServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/UserDeleteServlet")
public class UserDeleteServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        Employee employee = (Employee) session.getAttribute("employee");
        if (employee != null) {
            List menus1 = (List) session.getAttribute("Menus");
            List<SecondMenu> menuList = (List<SecondMenu>) menus1.get(1);
            for (SecondMenu secondMenu : menuList) {
                if (secondMenu.getS_menu_id() == 13) {
                    Integer employee_id = Integer.parseInt(request.getParameter("employee_id"));
                    EmployeeServiceImp employeeService = new EmployeeServiceImp();
                    if(employee_id!=employee.getEmployee_id()) {
                        employeeService.deleteEmployee(employee_id);
                        response.getWriter().print("删除成功");
                        List<Employee> employees = employeeService.getEmployees();
                        request.getSession().setAttribute("userlist",employees);}
                    else {
                        response.getWriter().print("不能删除本用户");
                    }
                    }
                }
            }
        }
    }
